package algo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;

import model.DebateFormat;
import model.Speaker;
import model.SpeakerRole;

/**
 * Represents the result of a distribution process. It contains several
 * <code>SpeakerDistribution</code>s which represent the distribution for a
 * single debate.
 * 
 * @author Stoeoef
 * 
 */
public class DistributionResult {

	// Contains the distributions for all debates
	private final List<SingleDebateDistribution> distributions;

	public static class SingleDebateDistribution {

		private final DebateFormat format;

		public DebateFormat getFormat() {
			return format;
		}

		private final List<Entry<SpeakerRole, Speaker>> mapping;

		public SingleDebateDistribution(DebateFormat format,
				List<Entry<SpeakerRole, Speaker>> mapping) {
			this.format = format;
			this.mapping = mapping;
		};

		public List<Speaker> getSpeakersForRole(SpeakerRole role) {
			ArrayList<Speaker> result = new ArrayList<>();
			for (Entry<SpeakerRole, Speaker> e : mapping) {
				if (e.getKey() == role) {
					result.add(e.getValue());
				}
			}
			return result;
		}

		public Speaker getFirstSpeakerForRole(SpeakerRole role) {
			for (Entry<SpeakerRole, Speaker> e : mapping) {
				if (e.getKey() == role) {
					return e.getValue();
				}
			}
			return null;
		}

		@Override
		public String toString() {
			return mapping.toString();
		}
	}

	public DistributionResult(SingleDebateDistribution[] distributions) {
		for (Object o : distributions) {
			if (o == null) {
				throw new IllegalArgumentException(
						"No distribution may be zero");
			}
		}
		this.distributions = Arrays.asList(distributions);
	}

	public List<SingleDebateDistribution> getDistributions() {
		return distributions;
	}

	@Override
	public String toString() {
		return distributions.toString();
	}
}
